package com.example.pagehelper.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.example.pagehelper.bean.User;
import com.example.pagehelper.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Controller
@Api("用户查询")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);


    @Autowired
    private UserService userService;

    @RequestMapping({"/","/index"})
    public String toIndex(){
        return "index";
    }
    @ApiOperation(value = "用户列表",notes = "通过名字查询用户")
    @RequestMapping("/queryUserByName")
    public String queryUserByName(Model model, @RequestParam("username") String user){
        List<User> users = userService.queryUserByName(user);
        model.addAttribute("users",users);
        return "/query/queryonesuccess";
    }

    @RequestMapping("/queryAllUser")
    @ApiOperation(value = "用户列表",notes = "查询所有用户")
    public String queryAllUser(Model model,
    @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                               @RequestParam(defaultValue="1",value="pageSize")Integer pageSize){
        //List<User> users = userService.queryAllUser();
        //model.addAttribute("users",users);

        if(pageNum == null){
            pageNum = 1;   //设置默认当前页
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 1;    //设置默认每页显示的数据数
        }
        System.out.println("当前页是："+pageNum+"显示条数是："+pageSize);

        //1.引入分页插件,pageNum是第几页，pageSize是每页显示多少条,默认查询总数count
        PageHelper.startPage(pageNum,pageSize);
        //2.紧跟的查询就是一个分页查询-必须紧跟.后面的其他查询不会被分页，除非再次调用PageHelper.startPage
        try {
            List<User> users = userService.queryAllUser();
            //3.使用PageInfo包装查询后的结果,5是连续显示的条数,结果list类型是Page<E>
            PageInfo<User> pageInfo = new PageInfo<User>(users,pageSize);
            //4.使用model/等带回前端
            model.addAttribute("pageInfo",pageInfo);
            //查询数据带回前端
            model.addAttribute("users",users);
        }finally {

            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }

        return "/query/querysuccess";
    }

    @ResponseBody
    @RequestMapping("/easyExcel")
    public void  exportExcel(HttpServletResponse response){
        log.info("请求导出为excel");

        List<User> users=userService.queryAllUser();
        try{
            //设置相应输出的头类型以及下载文件的默认名称
            String fileName = new String("demo信息表.xls".getBytes("utf-8"), "ISO-8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=gb2312");

            //导出
            Workbook workbook= ExcelExportUtil.exportExcel(new ExportParams(),User.class,users);
            workbook.write(response.getOutputStream());
            log.info("请求结束");

        } catch (IOException e) {
            log.info("请求异常");
        }


    }

}
